package cn.hfatec.shms.web.ss.ssHd.action;

import cn.hfatec.JsonResponse;
import cn.hfatec.Page;
import cn.hfatec.Table;
import cn.hfatec.constants.SysContant;
import cn.hfatec.freemarker.FreeMarkerUtil;
import cn.hfatec.shms.base.ShmsBaseAction;
import cn.hfatec.shms.web.ss.ssCustomer.model.SsCustomer;
import cn.hfatec.shms.web.ss.ssCustomer.model.SsCustomerParams;
import cn.hfatec.shms.web.ss.ssHd.model.SsHd;
import cn.hfatec.shms.web.ss.ssHd.model.SsHdParams;
import cn.hfatec.shms.web.ss.ssHd.service.SsHdService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;


/**
 * @author hfatec codegen
 * @version 1.0
 *          <p/>
 *          the JAVA code is generate by middlegen
 */
@Controller
@RequestMapping("/ss/ssHd")
public class SsHdAction extends ShmsBaseAction {

    //服务层
    @Resource(name = "SsHdService")
    private SsHdService ssHdService;


    /**
     * 查询列表
     */
    @RequestMapping("/list")
    public String list(SsHdParams ssHdParams, Page page, Model model) {

        Table table = ssHdService.querySsHdTable(ssHdParams, page);
        model.addAttribute("ssHdsTable", table);
        model.addAttribute("ssHdParams", ssHdParams);
        return "/ss/ssHd/list";
    }

    /**
     * 异步查询列表
     */
    @RequestMapping("/ajaxList")
    @ResponseBody
    public String ajaxList(SsHdParams ssHdParams, Page page, Model model) {
        try {
            list(ssHdParams, page, model);
            return new JsonResponse<Map>(JsonResponse.RETURN_SUCCESS, "", FreeMarkerUtil.render("/ss/ssHd/table", model)).toString();
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return JsonResponse.error;
    }

    /**
     * 跳转到新增页
     */
    @RequestMapping("/goAdd")
    @ResponseBody
    public String goAdd(Model model) {
        try {
            SsHd ssHd = new SsHd();
            model.addAttribute("ssHd", ssHd);
            return new JsonResponse<Map>(JsonResponse.RETURN_SUCCESS, "", FreeMarkerUtil.render("/ss/ssHd/form", model)).toString();

        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return JsonResponse.error;
    }

    /**
     * 跳转到编辑页
     */
    @RequestMapping("/goEdit")
    @ResponseBody
    public String goEdit(Integer id, Model model) {

        try {
            SsHd ssHd = ssHdService.getSsHd(id);
            model.addAttribute("ssHd", ssHd);
            return new JsonResponse<Map>(JsonResponse.RETURN_SUCCESS, "", FreeMarkerUtil.render("/ss/ssHd/form", model)).toString();

        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return JsonResponse.error;
    }

    /**
     * 根据IDs删除数据
     */
    @RequestMapping("/delete")
    @ResponseBody
    public String deleteSsHd(String ids) {
        try {
            List<String> idList = new ArrayList<String>();
            if (ids.length() > 0) {
                idList = Arrays.asList(ids.split(","));
            }
            ssHdService.deleteSsHdBatch(idList);
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("数据" + ids + "删除异常！");
            return new JsonResponse<String>(JsonResponse.RETURN_FAILURE, SysContant.DELETE_ERROR_MESSAGE).toString();
        }
        return new JsonResponse(JsonResponse.RETURN_SUCCESS, SysContant.DELETE_SUCCESS_MESSAGE).toString();
    }

    /**
     * 保存数据
     */
    @RequestMapping("/save")
    @ResponseBody
    public String saveSsHd(@Valid SsHd ssHd, BindingResult result) {

        try {
            if (result.hasErrors()) {
                List<ObjectError> errors = result.getAllErrors();
                return new JsonResponse<String>(JsonResponse.RETURN_FAILURE, errors.get(0).getDefaultMessage()).toString();
            }
            ssHdService.saveSsHd(ssHd);
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("数据SsHd保存异常！");
            return new JsonResponse<String>(JsonResponse.RETURN_FAILURE, SysContant.SAVE_ERROR_MESSAGE).toString();
        }
        return new JsonResponse<String>(JsonResponse.RETURN_SUCCESS, SysContant.SAVE_SUCCESS_MESSAGE).toString();
    }


    /**
     * 导出excel
     */
    @RequestMapping("/goCheck")
    @ResponseBody
    public String goCheck() {
        try {
            List<SsHd> selectSsHd = ssHdService.selectSshdAction();
            if (selectSsHd != null && !selectSsHd.isEmpty()) {
                ssHdService.deleteAllAction();
                for (SsHd selectHd : selectSsHd) {
                    ssHdService.saveSsHd(selectHd);
                }
            }
            return new JsonResponse(JsonResponse.RETURN_SUCCESS, "", new SsHd()).toString();

        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return JsonResponse.error;
    }

    /**
     * 导出excel
     */
    @RequestMapping("/export")
    @ResponseBody
    public String export(SsHdParams ssHdParams, HttpServletRequest request, HttpServletResponse response) {
        try {
            ssHdService.exportExcel(ssHdParams, request, response);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return JsonResponse.error;
    }
}
